package com.burgeon.backend.SHOPBACKEND.rest.order;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.OrderVO.OrderGoodsVO;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FindOrderDataITEM extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindOrderDataITEM(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 接收REP返回的字符串并进行数据处理
     *
     * @return
     */
    public List<OrderGoodsVO> getData(String id) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(id), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        List<OrderGoodsVO> orderGoodsVOList = new ArrayList<>();

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        OrderGoodsVO orderGoodsVO  = new OrderGoodsVO();



        return orderGoodsVOList;
    }

    /**
     * 准备向ERP传送字段拼接
     *
     * @return
     */
    private MultiValueMap Data(String id) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");//ID
        columns.add("RECEIVER_NAME");//收货人姓名
        columns.add("RECEIVER_MOBILE");//收货人手机号
        columns.add("RECEIVER_ADDRESS");// 收货人地址
        columns.add("'C_EXPRESSCOMPANY_ID;NAME'");// 快递公司
        columns.add("'EXPRESSCOMPANY_NO'");//快递单号
        columns.add("'IS_SELFRAIS'");//是否自提 配送方式
        columns.add("'TOT_AMT_LIST'");//商品总额
        columns.add("'TOT_AMT_ACTUAL'");//付款总额
        columns.add("'C_STORE_ID;NAME'");//发货店仓
        columns.add("'C_VIP_ID;VIPNAME'");//购买人姓名
        columns.add("'DESCRIPTION'");//备注

        params.put("columns", columns);
        params.put("table", "YC_RETAILITEM");//表名

        HashMap paramss = new HashMap();

        paramss.put("column", "YC_RETAIL_ID");
        paramss.put("condition", id);
        params.put("params", paramss);

        return DATA("Query", params, account, password);

    }
    /**
     * 执行方法，向ERP传参
     *
     * @param args
     */
    public static void main(String[] args) {
        FindOrderDataITEM queryOrder = new FindOrderDataITEM("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");

        try {
            System.err.println(queryOrder.getData("1000001875427129"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}